﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Company;
using BrokerBase.Lib.Core;

namespace Broker.View.CompanyView
{
    /// <summary>
    /// CompanyListPage.xaml 的交互逻辑
    /// </summary>
    public partial class CompanyListPage 
    {
        public CompanyListPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(CompanyListPage_Loaded);
        }

        void CompanyListPage_Loaded(object sender, RoutedEventArgs e)
        {
            SetOperRight();
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            Query();
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            ICompany cmp = new Company();

            CompanyForm frm = new CompanyForm(cmp);

            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.Owner = LibGlobal.MainWin;
            frm.ShowDialog();
            Query();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            ViewCompany();
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            ICompany cmp = gvCompany.SelectedItem as ICompany;
            if (cmp != null)
            {
                if (MessageBox.Show(string.Format("是否要删除[{0}]?", cmp.Name), "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    IBaseService<Company> svr =new  BaseService<Company>(LibGlobal.HbmSessionFactory.GetCurrentSession());

                    svr.Delete(cmp as Company);
                    Query();
                }
            }

        }

        private void Query()
        {
            string hql = "from Company as c where 1=1";

            if (!string.IsNullOrEmpty(txtName.Text))
                hql += " and c.FullName Like '" + txtName.Text.Trim() + "%' or c.Name Like '%" + txtName.Text.Trim() +"%'";

            IBaseService<Company> svr = new BaseService<Company>(LibGlobal.HbmSessionFactory.GetCurrentSession());

            IList<ICompany> cmps = svr.GetListByHQL(hql).Cast<ICompany>().ToList<ICompany>();

            gvCompany.ItemsSource = cmps;
            gvCompany.Items.Refresh();
        }

        private void gvCompany_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ViewCompany();
        }

        private void ViewCompany()
        {
            ICompany cmp = gvCompany.SelectedItem as ICompany;

            if (cmp != null)
            {
                CompanyForm frm = new CompanyForm(cmp);

                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = LibGlobal.MainWin;
                frm.ShowDialog();

                Query();
            }
        }

        private void SetOperRight()
        {
            btnNew.Visibility = LibGlobal.AppType == AppType.Manager ? Visibility.Visible : Visibility.Collapsed;
            btnDel.Visibility = LibGlobal.AppType == AppType.Manager ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}
